package com.infore.qualityControl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.qualityControl.bo.QualityParamSelectBo;
import com.infore.qualityControl.bo.QualityParamSetBo;
import com.infore.qualityControl.constant.Constants;
import com.infore.qualityControl.constant.ResultConstants;
import com.infore.qualityControl.dto.WaterInstructionsDataDto;
import com.infore.qualityControl.entity.DayQualityControl;
import com.infore.qualityControl.entity.EquipmentParameter;
import com.infore.qualityControl.entity.MonthQualityIntegrated;
import com.infore.qualityControl.entity.MonthQualityMultipoint;
import com.infore.qualityControl.entity.MonthQualityRealWater;
import com.infore.qualityControl.entity.MonthQualityRecovery;
import com.infore.qualityControl.entity.MonthQualityStandardCheck;
import com.infore.qualityControl.entity.WeekQualityControl;
import com.infore.qualityControl.enums.QualityAnalysisEnum;
import com.infore.qualityControl.enums.QualityProjectEnum;
import com.infore.qualityControl.enums.ResultStatus;
import com.infore.qualityControl.po.SpanDrift;
import com.infore.qualityControl.po.ZeroDrift;
import com.infore.qualityControl.remoteService.IDataReceivingService;
import com.infore.qualityControl.repository.IDayQualityControlRepository;
import com.infore.qualityControl.repository.IEquipmentParameterRepository;
import com.infore.qualityControl.repository.IFiveParameterRepository;
import com.infore.qualityControl.repository.IIntegratedInterventionRepository;
import com.infore.qualityControl.repository.IMonthQualityIntegratedRepository;
import com.infore.qualityControl.repository.IMonthQualityMultipointRepository;
import com.infore.qualityControl.repository.IMonthQualityRealWaterRepository;
import com.infore.qualityControl.repository.IMonthQualityRecoveryRepository;
import com.infore.qualityControl.repository.IMonthQualityStandardCheckRepository;
import com.infore.qualityControl.repository.IMultipointLinearityRepository;
import com.infore.qualityControl.repository.IRealWaterSampleComparisonRepository;
import com.infore.qualityControl.repository.IRecoveryRepository;
import com.infore.qualityControl.repository.ISpanDriftRepository;
import com.infore.qualityControl.repository.IVerificationOfStandardSampleRepository;
import com.infore.qualityControl.repository.IWeekQualityControlRepository;
import com.infore.qualityControl.repository.IZeroDriftRepository;
import com.infore.qualityControl.select.util.PageObject;
import com.infore.qualityControl.service.IQualityControlSetService;
import com.infore.qualityControl.service.IVerificationService;
import com.infore.qualityControl.util.ArrayUtils;
import com.infore.qualityControl.util.DateUtil;
import com.infore.qualityControl.util.ObjectMapperUtil;
import com.infore.qualityControl.util.Page;
import com.infore.qualityControl.util.PageUtil;
import com.infore.qualityControl.util.PolluteStandardUtil;
import com.infore.qualityControl.util.ResultEntity;

@Service
public class QualityControlSetServiceImpl implements IQualityControlSetService {

    @Autowired
    IDayQualityControlRepository IDayQualityControlRepository;

    @Autowired
    IWeekQualityControlRepository IWeekQualityControlRepository;

    @Autowired
    IMonthQualityMultipointRepository IMonthQualityMultipointRepository;

    @Autowired
    IMonthQualityIntegratedRepository IMonthQualityIntegratedRepository;

    @Autowired
    IMonthQualityRealWaterRepository IMonthQualityRealWaterRepository;

    @Autowired
    IMonthQualityRecoveryRepository IMonthQualityRecoveryRepository;

    @Autowired
    IMonthQualityStandardCheckRepository IMonthQualityStandardCheckRepository;

    @Autowired
    IEquipmentParameterRepository IEquipmentParameterRepository;

    @Autowired
    IZeroDriftRepository IZeroDriftRepository;

    @Autowired
    ISpanDriftRepository ISpanDriftRepository;

    @Autowired
    IFiveParameterRepository IFiveParameterRepository;

    @Autowired
    IMultipointLinearityRepository IMultipointLinearityRepository;

    @Autowired
    IIntegratedInterventionRepository IIntegratedInterventionRepository;

    @Autowired
    IRealWaterSampleComparisonRepository IRealWaterSampleComparisonRepository;

    @Autowired
    IRecoveryRepository IRecoveryRepository;

    @Autowired
    IVerificationOfStandardSampleRepository IVerificationOfStandardSampleRepository;

    @Autowired
    IVerificationService IVerificationService;

    @Autowired
    IDataReceivingService IDataReceivingService;

    @Autowired
    ObjectMapperUtil objectMapperUtil;

    public static final String AUDIT_FLAG = "1"; // 已审核标识
    public static final String NO_AUDIT_FLAG = "0"; // 未审核标识

    @Override
    public ResultEntity currentDayQualityControlParam(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        List<DayQualityControl> list = IDayQualityControlRepository.getLatestData(stationCodeList);
        // 获取该站点各监测项的总记录数
        List<Map<String, Object>> countList = IDayQualityControlRepository.getIndexCountData(stationCodeList);
        for (DayQualityControl dayQualityControl : list) {
            String indexCode = dayQualityControl.getIndexCode();
            for (Map<String, Object> map : countList) {
                if (indexCode.equals(map.get("INDEX_CODE"))) {
                    dayQualityControl.setIndexCount(map.get("COUNT").toString()); // 设置每项总记录数
                }
            }
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity historyDayQualityControlParam(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
        boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
        ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
        if (resultEntity.getCode() == 1) {
            return resultEntity;
        }
        // 将前端所传的yyyy-MM-dd 转成 yyyy-MM-dd HH:mm:ss
        Date startDate = DateUtil.dangyueDiyitianDay(DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR),
                DateUtil.DATE_FULL_STR);
        Date endDate = DateUtil.getLastDayDayM(DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR),
                DateUtil.DATE_FULL_STR);
        String indexCode = qualityParamSelectBo.getIndexCodes();
        List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        if (pageFlag) {
            Page<DayQualityControl> page = PageUtil.pageConvert(
                    IDayQualityControlRepository.findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDesc(startDate,
                            endDate, indexCode, stationCodeList, pageObject.getPageRequest()));
            return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
        }
        List<DayQualityControl> list = IDayQualityControlRepository
                .findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDesc(startDate, endDate, indexCode, stationCodeList);
        if (list.size() > 0) {
            // 抽取设置参数ID集合
            List<String> disposeList = list.stream().map(l -> l.getId().toString()).collect(Collectors.toList());
            // 获取设置参数使用的情况
            List<Map<String, Object>> zeroQualityList = IZeroDriftRepository.getQualityDisposeData(startDate, endDate, stationCodeList,
                    indexCode, disposeList);
            List<Map<String, Object>> spanQualityList = ISpanDriftRepository.getQualityDisposeData(startDate, endDate, stationCodeList,
                    indexCode, disposeList);
            for (DayQualityControl dayQualityControl : list) {
                String id = dayQualityControl.getId();
                for (Map<String, Object> map : zeroQualityList) {
                    String disposeId = map.get("QUALITY_DISPOSE_ID").toString(); // 质控设置参数ID
                    String time = map.get("TIME").toString(); // 最早的时间
                    Integer unqualifiedNumber = Integer.valueOf(map.get("UNQUALIFIEDNUMBER").toString()); // 不合格次数
                    Integer qualityNumber = Integer.valueOf(map.get("QUALITYNUMBER").toString()); // 质控次数
                    int operatingTime = DateUtil.differentDays(DateUtil.stringToDate(time, DateUtil.DATE_FULL_STR), new Date());
                    if (id.equals(disposeId)) {
                        dayQualityControl.setOperatingTime(operatingTime + "");
                        dayQualityControl.setQualityNumber(qualityNumber);
                        dayQualityControl.setUnqualifiedNumber(unqualifiedNumber);
                    }
                }
                for (Map<String, Object> map : spanQualityList) {
                    String disposeId = map.get("QUALITY_DISPOSE_ID").toString(); // 质控设置参数ID
                    String time = map.get("TIME").toString(); // 最早的时间
                    Integer unqualifiedNumber = Integer.valueOf(map.get("UNQUALIFIEDNUMBER").toString()); // 不合格次数
                    Integer qualityNumber = Integer.valueOf(map.get("QUALITYNUMBER").toString()); // 质控次数
                    int operatingTime = DateUtil.differentDays(DateUtil.stringToDate(time, DateUtil.DATE_FULL_STR), new Date());
                    if (id.equals(disposeId)) {
                        if (operatingTime >= Integer.valueOf(dayQualityControl.getOperatingTime())) {
                            dayQualityControl.setOperatingTime(operatingTime + "天");
                        } else {
                            dayQualityControl.setOperatingTime(dayQualityControl.getOperatingTime() + "天");
                        }
                        dayQualityControl.setOperatingTime(operatingTime + "天");
                        dayQualityControl.setQualityNumber(dayQualityControl.getQualityNumber() + qualityNumber);
                        dayQualityControl.setUnqualifiedNumber(dayQualityControl.getUnqualifiedNumber() + unqualifiedNumber);
                    }
                }
            }
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity editorCurrentDayQualityControlParam(List<QualityParamSetBo> qualityParamSetBoList) {
        for (QualityParamSetBo qualityParamSetBo : qualityParamSetBoList) {
            DayQualityControl dayQualityControl = new DayQualityControl();
            dayQualityControl.setStationCode(qualityParamSetBo.getStationCodes());
            dayQualityControl.setIndexCode(qualityParamSetBo.getIndexCode());
            dayQualityControl.setIndexName(QualityProjectEnum.getName(qualityParamSetBo.getIndexCode()));
            dayQualityControl.setIndexUnit(QualityProjectEnum.getUnit(qualityParamSetBo.getIndexCode()));
            dayQualityControl.setSpanValue(qualityParamSetBo.getSpanValue());
            dayQualityControl.setZeroStandardValue(qualityParamSetBo.getZeroStandardValue());
            dayQualityControl.setSpanStandardValue(qualityParamSetBo.getSpanStandardValue());
            dayQualityControl.setSubmitTime(new Date());
            // 缺少提交人
            dayQualityControl.setAuditFlag(NO_AUDIT_FLAG);
            IDayQualityControlRepository.saveAndFlush(dayQualityControl);
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.INSERT_SUCCESS);
    }

    @Override
    public ResultEntity auditCurrentDayQualityControlParam(String id) {
        // 这里使用findById而不使用existsById查询，是因为existsById返回的是boolean型,而下面判断是需要实体对象
        DayQualityControl oldDayQualityControl = IDayQualityControlRepository.findById(id).orElse(null);
        if (oldDayQualityControl != null) {
            oldDayQualityControl.setAuditFlag(AUDIT_FLAG);
            // 缺少审核人
            oldDayQualityControl.setAuditTime(new Date());
        } else {
            // 数据对象不存在
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.NO_SUCH_OBJECT);
        }
        // 保存
        IDayQualityControlRepository.saveAndFlush(oldDayQualityControl);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_SUCCESS);
    }

    @Override
    public ResultEntity currentWeekQualityControlParam(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        List<String> stationList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        List<WeekQualityControl> list = IWeekQualityControlRepository.getLatestData(stationList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity historyWeekQualityControlParam(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
        boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
        ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
        if (resultEntity.getCode() == 1) {
            return resultEntity;
        }
        // 将前端所传的yyyy-MM-dd 转成 yyyy-MM-dd HH:mm:ss
        Date startDate = DateUtil.dangyueDiyitianDay(DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR),
                DateUtil.DATE_FULL_STR);
        Date endDate = DateUtil.getLastDayDayM(DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR),
                DateUtil.DATE_FULL_STR);
        String indexCode = qualityParamSelectBo.getIndexCodes();
        List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        if (pageFlag) {
            Page<WeekQualityControl> page = PageUtil.pageConvert(
                    IWeekQualityControlRepository.findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDesc(startDate,
                            endDate, indexCode, stationCodeList, pageObject.getPageRequest()));
            return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
        }
        List<WeekQualityControl> list = IWeekQualityControlRepository
                .findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDesc(startDate, endDate, indexCode, stationCodeList);
        // 数据封装,增加时间周数体现
        for (WeekQualityControl weekQualityControl : list) {
            String submitTime = DateUtil.dateToString(weekQualityControl.getSubmitTime(), DateUtil.DATE_SMALL_STR);
            int month = DateUtil.getMonth(weekQualityControl.getSubmitTime());
            int week = DateUtil.getMonthWeek(submitTime);
            weekQualityControl.setMonthWeek(month + "月第" + week + "周");
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity editorCurrentWeekQualityControlParam(List<QualityParamSetBo> qualityParamSetBoList) {
        for (QualityParamSetBo qualityParamSetBo : qualityParamSetBoList) {
            WeekQualityControl weekQualityControl = new WeekQualityControl();
            weekQualityControl.setStationCode(qualityParamSetBo.getStationCodes());
            weekQualityControl.setIndexCode(qualityParamSetBo.getIndexCode());
            weekQualityControl.setIndexName(QualityProjectEnum.getName(qualityParamSetBo.getIndexCode()));
            weekQualityControl.setIndexUnit(QualityProjectEnum.getUnit(qualityParamSetBo.getIndexCode()));
            weekQualityControl.setStandardValue(qualityParamSetBo.getStandardValue());
            weekQualityControl.setSubmitTime(new Date());
            IWeekQualityControlRepository.saveAndFlush(weekQualityControl);
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.INSERT_SUCCESS);
    }

    @Override
    public ResultEntity auditCurrentWeekQualityControlParam(String id) {
        WeekQualityControl oldWeekQualityControl = IWeekQualityControlRepository.findById(id).orElse(null);
        if (oldWeekQualityControl != null) {
            oldWeekQualityControl.setAuditFlag(AUDIT_FLAG);
            // 缺少审核人
            oldWeekQualityControl.setAuditTime(new Date());
        } else {
            // 数据对象不存在
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.NO_SUCH_OBJECT);
        }
        // 保存
        IWeekQualityControlRepository.saveAndFlush(oldWeekQualityControl);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_SUCCESS);
    }

    @Override
    public ResultEntity currentMonthQualityControlMultipointParam(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        List<String> stationList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        List<MonthQualityMultipoint> list = IMonthQualityMultipointRepository.getLatestData(stationList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @SuppressWarnings("unchecked")
    @Override
    public ResultEntity historyMonthQualityControlMultipointParam(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
        boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
        ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
        if (resultEntity.getCode() == 1) {
            return resultEntity;
        }
        Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR);
        Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR);
        String indexCode = qualityParamSelectBo.getIndexCodes();
        List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        if (pageFlag) {
            Page<MonthQualityMultipoint> page = PageUtil.pageConvert(
                    IMonthQualityMultipointRepository.findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDescTestPointAsc(
                            startDate, endDate, indexCode, stationCodeList, pageObject.getPageRequest()));
            return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
        }
        List<MonthQualityMultipoint> list = IMonthQualityMultipointRepository
                .findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDescTestPointAsc(startDate, endDate, indexCode,
                        stationCodeList);
        // 数据封装
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        Map<String, Object> valueMap = new HashMap<String, Object>();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> pointList = new ArrayList<Map<String, Object>>();
        for (MonthQualityMultipoint monthQualityMultipoint : list) {
            // yyyy-MM
            String submitTimeMonth = DateUtil.dateToString(monthQualityMultipoint.getSubmitTime(), DateUtil.DATE_SMALL_YM);
            Double standardValue = monthQualityMultipoint.getStandardValue();
            String submitPerson = monthQualityMultipoint.getSubmitPerson();
            String submitTimeDay = DateUtil.dateToString(monthQualityMultipoint.getSubmitTime(), "dd HH:mm");
            String auditTime = "";
            if (monthQualityMultipoint.getAuditTime() != null) {
                auditTime = DateUtil.dateToString(monthQualityMultipoint.getAuditTime(), "dd HH:mm");
            }
            if (!valueMap.containsKey(submitTimeMonth)) {
                // 封装每月的数据
                pointList = new ArrayList<Map<String, Object>>();
                resultMap = new HashMap<String, Object>();
                resultMap.put("monthTime", submitTimeMonth);
                resultMap.put("list", pointList);
                result.add(resultMap);
                valueMap.put(submitTimeMonth, pointList);
            } else {
                pointList = (List<Map<String, Object>>) valueMap.get(submitTimeMonth);
            }
            // 遍历封装每个点的数据
            Map<String, Object> pointMap = new HashMap<String, Object>();
            pointMap.put("standardValue", standardValue);
            pointMap.put("submitPerson", submitPerson);
            pointMap.put("submitTimeDay", submitTimeDay);
            pointMap.put("auditTime", auditTime);
            pointList.add(pointMap);
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
    }

    @Override
    public ResultEntity monthQualityControlMultipointParamZeroDrift(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        if (StringUtils.isBlank(qualityParamSelectBo.getIndexCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_INDEX);
        }
        List<ZeroDrift> list = IZeroDriftRepository.getLatestData(qualityParamSelectBo.getStationCodes(),
                qualityParamSelectBo.getIndexCodes());
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity monthQualityControlMultipointParamSpanDrift(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        if (StringUtils.isBlank(qualityParamSelectBo.getIndexCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_INDEX);
        }
        List<SpanDrift> list = ISpanDriftRepository.getLatestData(qualityParamSelectBo.getStationCodes(),
                qualityParamSelectBo.getIndexCodes());
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity editorCurrentMonthQualityControlMultipointParam(List<QualityParamSetBo> qualityParamSetBoList) {
        for (QualityParamSetBo qualityParamSetBo : qualityParamSetBoList) {
            List<Double> pointValues = qualityParamSetBo.getPointValues();
            List<String> sourceValues = qualityParamSetBo.getSourceValues();
            for (int i = 0; i < pointValues.size(); i++) {
                MonthQualityMultipoint monthQualityMultipoint = new MonthQualityMultipoint();
                monthQualityMultipoint.setStationCode(qualityParamSetBo.getStationCodes());
                monthQualityMultipoint.setIndexCode(qualityParamSetBo.getIndexCode());
                monthQualityMultipoint.setIndexName(QualityProjectEnum.getName(qualityParamSetBo.getIndexCode()));
                monthQualityMultipoint.setIndexUnit(QualityProjectEnum.getUnit(qualityParamSetBo.getIndexCode()));
                monthQualityMultipoint.setTestPoint((i + 1) + "");
                monthQualityMultipoint.setSource(sourceValues.get(i));
                monthQualityMultipoint.setStandardValue(pointValues.get(i));
                monthQualityMultipoint.setSubmitTime(new Date());
                IMonthQualityMultipointRepository.saveAndFlush(monthQualityMultipoint);
            }
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.INSERT_SUCCESS);
    }

    @Override
    public ResultEntity auditCurrentMonthQualityControlMultipointParam(String id) {
        MonthQualityMultipoint oldMonthQualityMultipoint = IMonthQualityMultipointRepository.findById(id).orElse(null);
        if (oldMonthQualityMultipoint != null) {
            oldMonthQualityMultipoint.setAuditFlag(AUDIT_FLAG);
            // 缺少审核人
            oldMonthQualityMultipoint.setAuditTime(new Date());
        } else {
            // 数据对象不存在
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.NO_SUCH_OBJECT);
        }
        // 保存
        IMonthQualityMultipointRepository.saveAndFlush(oldMonthQualityMultipoint);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_SUCCESS);
    }

    @Override
    public ResultEntity currentMonthQualityControlIntegratedParam(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        List<String> stationList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        List<MonthQualityIntegrated> list = IMonthQualityIntegratedRepository.getLatestData(stationList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity historyMonthQualityControlIntegratedParam(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
        boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
        ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
        if (resultEntity.getCode() == 1) {
            return resultEntity;
        }
        Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR);
        Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR);
        String indexCode = qualityParamSelectBo.getIndexCodes();
        List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        if (pageFlag) {
            Page<MonthQualityIntegrated> page = PageUtil.pageConvert(
                    IMonthQualityIntegratedRepository.findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDesc(startDate,
                            endDate, indexCode, stationCodeList, pageObject.getPageRequest()));
            return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
        }
        List<MonthQualityIntegrated> list = IMonthQualityIntegratedRepository
                .findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDesc(startDate, endDate, indexCode, stationCodeList);
        // 数据封装,增加时间周数体现
        for (MonthQualityIntegrated monthQualityIntegrated : list) {
            String submitTime = DateUtil.dateToString(monthQualityIntegrated.getSubmitTime(), DateUtil.DATE_SMALL_STR);
            int month = DateUtil.getMonth(monthQualityIntegrated.getSubmitTime());
            int week = DateUtil.getMonthWeek(submitTime);
            monthQualityIntegrated.setMonthWeek(month + "月第" + week + "周");
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity editorCurrentMonthQualityControlIntegratedParam(List<QualityParamSetBo> qualityParamSetBoList) {
        for (QualityParamSetBo qualityParamSetBo : qualityParamSetBoList) {
            MonthQualityIntegrated monthQualityIntegrated = new MonthQualityIntegrated();
            monthQualityIntegrated.setStationCode(qualityParamSetBo.getStationCodes());
            monthQualityIntegrated.setIndexCode(qualityParamSetBo.getIndexCode());
            monthQualityIntegrated.setIndexName(QualityProjectEnum.getName(qualityParamSetBo.getIndexCode()));
            monthQualityIntegrated.setIndexUnit(QualityProjectEnum.getUnit(qualityParamSetBo.getIndexCode()));
            monthQualityIntegrated
                    .setNotIntegratedTime(DateUtil.stringToDate(qualityParamSetBo.getNotIntegratedTime(), DateUtil.DATE_FULL_STR));
            monthQualityIntegrated
                    .setOverIntegratedTime(DateUtil.stringToDate(qualityParamSetBo.getOverIntegratedTime(), DateUtil.DATE_FULL_STR));
            monthQualityIntegrated.setSubmitTime(new Date());
            IMonthQualityIntegratedRepository.saveAndFlush(monthQualityIntegrated);
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.INSERT_SUCCESS);
    }

    @Override
    public ResultEntity auditCurrentMonthQualityControlIntegratedParam(String id) {
        MonthQualityIntegrated oldMonthQualityIntegrated = IMonthQualityIntegratedRepository.findById(id).orElse(null);
        if (oldMonthQualityIntegrated != null) {
            oldMonthQualityIntegrated.setAuditFlag(AUDIT_FLAG);
            // 缺少审核人
            oldMonthQualityIntegrated.setAuditTime(new Date());
        } else {
            // 数据对象不存在
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.NO_SUCH_OBJECT);
        }
        // 保存
        IMonthQualityIntegratedRepository.saveAndFlush(oldMonthQualityIntegrated);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_SUCCESS);
    }

    @Override
    public ResultEntity currentMonthQualityControlRealWaterParam(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        List<String> stationList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        List<MonthQualityRealWater> list = IMonthQualityRealWaterRepository.getLatestData(stationList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity editorCurrentMonthQualityControlRealWaterParam(List<QualityParamSetBo> qualityParamSetBoList) {
        for (QualityParamSetBo qualityParamSetBo : qualityParamSetBoList) {
            MonthQualityRealWater monthQualityRealWater = new MonthQualityRealWater();
            monthQualityRealWater.setStationCode(qualityParamSetBo.getStationCodes());
            monthQualityRealWater.setIndexCode(qualityParamSetBo.getIndexCode());
            monthQualityRealWater.setIndexName(QualityProjectEnum.getName(qualityParamSetBo.getIndexCode()));
            monthQualityRealWater.setIndexUnit(QualityProjectEnum.getUnit(qualityParamSetBo.getIndexCode()));
            monthQualityRealWater.setTestTime(DateUtil.stringToDate(qualityParamSetBo.getTestTime(), DateUtil.DATE_FULL_STR));
            monthQualityRealWater.setSubmitTime(new Date());
            IMonthQualityRealWaterRepository.saveAndFlush(monthQualityRealWater);
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.INSERT_SUCCESS);
    }

    @Override
    public ResultEntity auditCurrentMonthQualityControlRealWaterParam(String id) {
        MonthQualityRealWater oldMonthQualityRealWater = IMonthQualityRealWaterRepository.findById(id).orElse(null);
        if (oldMonthQualityRealWater != null) {
            oldMonthQualityRealWater.setAuditFlag(AUDIT_FLAG);
            // 缺少审核人
            oldMonthQualityRealWater.setAuditTime(new Date());
        } else {
            // 数据对象不存在
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.NO_SUCH_OBJECT);
        }
        // 保存
        IMonthQualityRealWaterRepository.saveAndFlush(oldMonthQualityRealWater);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_SUCCESS);
    }

    @Override
    public ResultEntity currentMonthQualityControlRecoveryParam(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        List<String> stationList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        List<MonthQualityRecovery> list = IMonthQualityRecoveryRepository.getLatestData(stationList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity historyMonthQualityControlRecoveryParam(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
        boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
        ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
        if (resultEntity.getCode() == 1) {
            return resultEntity;
        }
        Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR);
        Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR);
        String indexCode = qualityParamSelectBo.getIndexCodes();
        List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        if (pageFlag) {
            Page<MonthQualityRecovery> page = PageUtil.pageConvert(
                    IMonthQualityRecoveryRepository.findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDesc(startDate,
                            endDate, indexCode, stationCodeList, pageObject.getPageRequest()));
            return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
        }
        List<MonthQualityRecovery> list = IMonthQualityRecoveryRepository
                .findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDesc(startDate, endDate, indexCode, stationCodeList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity editorCurrentMonthQualityControlRecoveryParam(List<QualityParamSetBo> qualityParamSetBoList) {
        for (QualityParamSetBo qualityParamSetBo : qualityParamSetBoList) {
            MonthQualityRecovery monthQualityRecovery = new MonthQualityRecovery();
            monthQualityRecovery.setStationCode(qualityParamSetBo.getStationCodes());
            monthQualityRecovery.setIndexCode(qualityParamSetBo.getIndexCode());
            monthQualityRecovery.setIndexName(QualityProjectEnum.getName(qualityParamSetBo.getIndexCode()));
            monthQualityRecovery.setIndexUnit(QualityProjectEnum.getUnit(qualityParamSetBo.getIndexCode()));
            monthQualityRecovery.setAddConcentration(qualityParamSetBo.getAddConcentration());
            monthQualityRecovery.setConstantVolume(qualityParamSetBo.getConstantVolume());
            monthQualityRecovery.setSubmitTime(new Date());
            IMonthQualityRecoveryRepository.saveAndFlush(monthQualityRecovery);
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.INSERT_SUCCESS);
    }

    @Override
    public ResultEntity auditCurrentMonthQualityControlRecoveryParam(String id) {
        MonthQualityRecovery oldMonthQualityRecovery = IMonthQualityRecoveryRepository.findById(id).orElse(null);
        if (oldMonthQualityRecovery != null) {
            oldMonthQualityRecovery.setAuditFlag(AUDIT_FLAG);
            // 缺少审核人
            oldMonthQualityRecovery.setAuditTime(new Date());
        } else {
            // 数据对象不存在
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.NO_SUCH_OBJECT);
        }
        // 保存
        IMonthQualityRecoveryRepository.saveAndFlush(oldMonthQualityRecovery);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_SUCCESS);
    }

    @Override
    public ResultEntity currentMonthQualityControlStandardCheckParam(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        List<String> stationList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        List<MonthQualityStandardCheck> list = IMonthQualityStandardCheckRepository.getLatestData(stationList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity historyMonthQualityControlStandardCheckParam(QualityParamSelectBo qualityParamSelectBo, PageObject pageObject) {
        boolean pageFlag = pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null;
        ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
        if (resultEntity.getCode() == 1) {
            return resultEntity;
        }
        Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR);
        Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR);
        String indexCode = qualityParamSelectBo.getIndexCodes();
        List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        if (pageFlag) {
            Page<MonthQualityStandardCheck> page = PageUtil.pageConvert(
                    IMonthQualityStandardCheckRepository.findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDesc(startDate,
                            endDate, indexCode, stationCodeList, pageObject.getPageRequest()));
            return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
        }
        List<MonthQualityStandardCheck> list = IMonthQualityStandardCheckRepository
                .findBySubmitTimeBetweenAndIndexCodeAndStationCodeInOrderBySubmitTimeDesc(startDate, endDate, indexCode, stationCodeList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity editorCurrentMonthQualityControlStandardCheckParam(List<QualityParamSetBo> qualityParamSetBoList) {
        for (QualityParamSetBo qualityParamSetBo : qualityParamSetBoList) {
            MonthQualityStandardCheck monthQualityStandardCheck = new MonthQualityStandardCheck();
            monthQualityStandardCheck.setStationCode(qualityParamSetBo.getStationCodes());
            monthQualityStandardCheck.setIndexCode(qualityParamSetBo.getIndexCode());
            monthQualityStandardCheck.setIndexName(QualityProjectEnum.getName(qualityParamSetBo.getIndexCode()));
            monthQualityStandardCheck.setIndexUnit(QualityProjectEnum.getUnit(qualityParamSetBo.getIndexCode()));
            monthQualityStandardCheck.setSpanValue(qualityParamSetBo.getSpanValue());
            monthQualityStandardCheck.setStandardValue(qualityParamSetBo.getStandardValue());
            monthQualityStandardCheck.setSubmitTime(new Date());
            IMonthQualityStandardCheckRepository.saveAndFlush(monthQualityStandardCheck);
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.INSERT_SUCCESS);
    }

    @Override
    public ResultEntity auditCurrentMonthQualityControlStandardCheckParam(String id) {
        MonthQualityStandardCheck oldMonthQualityStandardCheck = IMonthQualityStandardCheckRepository.findById(id).orElse(null);
        if (oldMonthQualityStandardCheck != null) {
            oldMonthQualityStandardCheck.setAuditFlag(AUDIT_FLAG);
            // 缺少审核人
            oldMonthQualityStandardCheck.setAuditTime(new Date());
        } else {
            // 数据对象不存在
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.NO_SUCH_OBJECT);
        }
        // 保存
        IMonthQualityStandardCheckRepository.saveAndFlush(oldMonthQualityStandardCheck);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_SUCCESS);
    }

    @Override
    public ResultEntity getEquipmentParameter(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        List<String> stationList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        List<EquipmentParameter> list = IEquipmentParameterRepository.findByStationCodeIn(stationList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity editorEquipmentParameter(String parameterId, String parameterValue) {
        if (StringUtils.isBlank(parameterId)) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER);
        }
        // 这里使用findById而不使用existsById查询，是因为existsById返回的是boolean型,而下面判断是需要实体对象
        // EquipmentParameter equipmentParameter =
        // IEquipmentParameterRepository.findById(parameterId).orElse(null);
        int updateNum = IEquipmentParameterRepository.updateParameterValue(parameterValue, parameterId);
        if (updateNum == 1) {
            return new ResultEntity(ResultStatus.SUCCESS, Constants.UPDATE_SUCCESS);
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.UPDATE_FAILUE);
    }

    @Override
    public ResultEntity getStationCurrentWaterQualityData(String stationCode) {
        ResultEntity resultEntity = IDataReceivingService.queryRealTimeWaterQualitySort(stationCode);
        List<WaterInstructionsDataDto> list = objectMapperUtil.convertList(resultEntity, WaterInstructionsDataDto.class);
        if ("40000000000003".equals(stationCode) || "40000000000004".equals(stationCode)) { // 污染源站点
            PolluteStandardUtil.setPolluteStandard(list); // 污染源污染标准
        }
        String waterQuality = null;
        for (WaterInstructionsDataDto waterInstructionsDataDto : list) {
            waterQuality = waterInstructionsDataDto.getWaterQuality();
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setInfo(waterQuality);
    }

    @Override
    public ResultEntity qualityControlConclusion(QualityParamSelectBo qualityParamSelectBo) {
        ResultEntity resultEntity = IVerificationService.verification(qualityParamSelectBo);
        if (resultEntity.getCode() == 1) {
            return resultEntity;
        }
        // 方法内部自己判定(如果放在通用判定里,其余接口会失效)
        if (StringUtils.isBlank(qualityParamSelectBo.getQualityAnalysisType())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_QUALITY_TYPE);
        }
        Date startDate = DateUtil.dangyueDiyitianDay(DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_STR),
                DateUtil.DATE_FULL_STR);
        Date endDate = DateUtil.getLastDayDayM(DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_STR),
                DateUtil.DATE_FULL_STR);
        List<String> indexCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getIndexCodes());
        List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        List<Map<String, Object>> list = null;
        if (QualityAnalysisEnum.WEEK_QUALITY.getCode().equals(qualityParamSelectBo.getQualityAnalysisType())) { // 周质控
            list = IFiveParameterRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
        } else if (QualityAnalysisEnum.MULTIPOINT_LINEARITY.getCode().equals(qualityParamSelectBo.getQualityAnalysisType())) { // 多点线性
            // list = IMultipointLinearityRepository.getQualifiedData(startDate, endDate,
            // stationCodeList, indexCodeList);
        } else if (QualityAnalysisEnum.INTEGRATED_INTERVENTION.getCode().equals(qualityParamSelectBo.getQualityAnalysisType())) { // 集成干预
            list = IIntegratedInterventionRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
        } else if (QualityAnalysisEnum.RECOVERY.getCode().equals(qualityParamSelectBo.getQualityAnalysisType())) { // 加标回收
            list = IRecoveryRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
        } else if (QualityAnalysisEnum.VERIFICATION_STANDARD_SAMPLES.getCode().equals(qualityParamSelectBo.getQualityAnalysisType())) { // 标样核查
            list = IVerificationOfStandardSampleRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (list != null && list.size() > 0) {
            for (Map<String, Object> map : list) {
                resultMap.put("count", map.get("COUNT").toString());
                resultMap.put("qualified", map.get("QUALIFIED").toString());
                resultMap.put("unqualified", map.get("UNQUALIFIED").toString());
            }
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setInfo(resultMap);
    }

    @Override
    public ResultEntity qualityControlTips(QualityParamSelectBo qualityParamSelectBo) {
        if (StringUtils.isBlank(qualityParamSelectBo.getStationCodes())) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        String qualityAnalysisType = qualityParamSelectBo.getQualityAnalysisType();
        String stationCode = qualityParamSelectBo.getStationCodes();
        Map<String, Object> resultMap = new HashMap<String, Object>(); // 返回的结果集
        // 根据质控类型获取不同的最新参数配置的时间
        List<Map<String, Object>> qualitySetUpList = new ArrayList<Map<String, Object>>(); // 质控设置的结果集
        List<Map<String, Object>> qualityDataList = new ArrayList<Map<String, Object>>(); // 质控数据的结果集
        if (QualityAnalysisEnum.DAY_QUALITY.getCode().equals(qualityAnalysisType)) { // 日质控
            qualitySetUpList = IDayQualityControlRepository.getSingleLatestData(stationCode);
            // qualityDataList = IFiveParameterRepository.getSingleLatestData(stationCode);
        } else if (QualityAnalysisEnum.WEEK_QUALITY.getCode().equals(qualityAnalysisType)) { // 周质控
            qualitySetUpList = IWeekQualityControlRepository.getSingleLatestData(stationCode);
            qualityDataList = IFiveParameterRepository.getSingleLatestData(stationCode);
        } else if (QualityAnalysisEnum.MULTIPOINT_LINEARITY.getCode().equals(qualityAnalysisType)) { // 多点线性
            qualitySetUpList = IMonthQualityMultipointRepository.getSingleLatestData(stationCode);
            qualityDataList = IMultipointLinearityRepository.getSingleLatestData(stationCode);
        } else if (QualityAnalysisEnum.INTEGRATED_INTERVENTION.getCode().equals(qualityAnalysisType)) { // 集成干预
            qualitySetUpList = IMonthQualityIntegratedRepository.getSingleLatestData(stationCode);
            qualityDataList = IIntegratedInterventionRepository.getSingleLatestData(stationCode);
        } else if (QualityAnalysisEnum.REAL_WATER_SAMPLE_COMPARISON.getCode().equals(qualityAnalysisType)) { // 实际水样
            qualitySetUpList = IMonthQualityRealWaterRepository.getSingleLatestData(stationCode);
            qualityDataList = IRealWaterSampleComparisonRepository.getSingleLatestData(stationCode);
        } else if (QualityAnalysisEnum.RECOVERY.getCode().equals(qualityAnalysisType)) { // 加标回收
            qualitySetUpList = IMonthQualityRecoveryRepository.getSingleLatestData(stationCode);
            qualityDataList = IRecoveryRepository.getSingleLatestData(stationCode);
        } else if (QualityAnalysisEnum.VERIFICATION_STANDARD_SAMPLES.getCode().equals(qualityAnalysisType)) { // 标样核查
            qualitySetUpList = IMonthQualityStandardCheckRepository.getSingleLatestData(stationCode);
            qualityDataList = IVerificationOfStandardSampleRepository.getSingleLatestData(stationCode);
        }
        String setUpLatestTime = null; // 质控参数配置最新的时间
        List<String> indexCodeList = new ArrayList<String>(); // 质控参数配置最新时间的因子编码
        List<String> disposeidList = new ArrayList<String>(); // 质控参数配置最新时间的ID(用来查询配置的参数被使用的情况)
        if (qualitySetUpList != null && qualitySetUpList.size() > 0) {
            for (Map<String, Object> map : qualitySetUpList) {
                if (map != null) {
                    setUpLatestTime = map.get("SUBMIT_TIME") == null ? "" : map.get("SUBMIT_TIME").toString();
                    indexCodeList.add(map.get("INDEX_CODE").toString());
                    disposeidList.add(map.get("ID") == null ? "" : map.get("ID").toString());
                }
            }
        }
        String dataLatestTime = null; // 质控数据最新的时间
        if (qualityDataList != null && qualityDataList.size() > 0) {
            for (Map<String, Object> map : qualityDataList) {
                if (map != null) {
                    dataLatestTime = map.get("TIME") == null ? "" : map.get("TIME").toString();
                }
            }
        }
        Date dataLatestDate = null;
        if (dataLatestTime != null && !"".equals(dataLatestTime)) {
            dataLatestDate = DateUtil.stringToDate(dataLatestTime, DateUtil.DATE_SMALL_STR);
        }
        List<String> stationCodeList = ArrayUtils.stringToList(qualityParamSelectBo.getStationCodes());
        List<Map<String, Object>> qualityList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> spanQualityList = new ArrayList<Map<String, Object>>();
        Date setUpLatestDate = null;
        if (setUpLatestTime != null && !"".equals(setUpLatestTime)) {
            Date startDate = DateUtil.dangyueDiyitianDay(DateUtil.stringToDate(setUpLatestTime, DateUtil.DATE_SMALL_STR),
                    DateUtil.DATE_FULL_STR);
            Date endDate = DateUtil.getLastDayDayM(new Date(), DateUtil.DATE_FULL_STR);
            // 根据质控类型获取不同的质控数据
            if (QualityAnalysisEnum.DAY_QUALITY.getCode().equals(qualityAnalysisType)) { // 日质控
                qualityList = IZeroDriftRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
                spanQualityList = ISpanDriftRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
            } else if (QualityAnalysisEnum.WEEK_QUALITY.getCode().equals(qualityAnalysisType)) { // 周质控
                qualityList = IFiveParameterRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
            } else if (QualityAnalysisEnum.MULTIPOINT_LINEARITY.getCode().equals(qualityAnalysisType)) { // 多点线性
                qualityList = IMultipointLinearityRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
            } else if (QualityAnalysisEnum.INTEGRATED_INTERVENTION.getCode().equals(qualityAnalysisType)) { // 集成干预
                qualityList = IIntegratedInterventionRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
            } else if (QualityAnalysisEnum.REAL_WATER_SAMPLE_COMPARISON.getCode().equals(qualityAnalysisType)) { // 实际水样
                qualityList = IRealWaterSampleComparisonRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
            } else if (QualityAnalysisEnum.RECOVERY.getCode().equals(qualityAnalysisType)) { // 加标回收
                qualityList = IRecoveryRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
            } else if (QualityAnalysisEnum.VERIFICATION_STANDARD_SAMPLES.getCode().equals(qualityAnalysisType)) { // 标样核查
                qualityList = IVerificationOfStandardSampleRepository.getQualifiedData(startDate, endDate, stationCodeList, indexCodeList);
            }
            setUpLatestDate = DateUtil.stringToDate(setUpLatestTime, DateUtil.DATE_SMALL_STR);
        }
        Integer qualified = 0; // 总合格次数
        Integer unqualified = 0; // 总不合格次数
        if (QualityAnalysisEnum.DAY_QUALITY.getCode().equals(qualityAnalysisType)) { // 日质控需要作特殊处理
            if (qualityList.size() > 0 || spanQualityList.size() > 0) {
                for (Map<String, Object> map : qualityList) {
                    Integer indexQualified = 0, indexUnqualified = 0;
                    String zeroIndexCode = map.get("INDEX_CODE") == null ? "" : map.get("INDEX_CODE").toString();
                    Integer zeroQualified = Integer.valueOf(map.get("QUALIFIED") == null ? "0" : map.get("QUALIFIED").toString());
                    Integer zeroUnqualified = Integer.valueOf(map.get("UNQUALIFIED") == null ? "0" : map.get("UNQUALIFIED").toString());
                    for (Map<String, Object> spanMap : spanQualityList) {
                        String spanIndexCode = spanMap.get("INDEX_CODE") == null ? "" : spanMap.get("INDEX_CODE").toString();
                        Integer spanQualified = Integer
                                .valueOf(spanMap.get("QUALIFIED") == null ? "0" : spanMap.get("QUALIFIED").toString());
                        Integer spanUnqualified = Integer
                                .valueOf(spanMap.get("UNQUALIFIED") == null ? "0" : spanMap.get("UNQUALIFIED").toString());
                        if (zeroIndexCode.equals(spanIndexCode)) {
                            indexUnqualified = zeroUnqualified >= spanUnqualified ? zeroUnqualified : spanUnqualified; // 不合格数量取最大值
                            indexQualified = zeroQualified <= spanQualified ? zeroQualified : spanQualified; // 合格数量取最小值
                            break;
                        }
                    }
                    qualified += indexQualified;
                    unqualified += indexUnqualified;
                }
                resultMap.put("QUALIFIED", qualified);
                resultMap.put("UNQUALIFIED", unqualified);
            } else {
                resultMap.put("QUALIFIED", "--");
                resultMap.put("UNQUALIFIED", "--");
            }
        } else {
            if (qualityList != null && qualityList.size() > 0) {
                for (Map<String, Object> map : qualityList) {
                    qualified += Integer.valueOf(map.get("QUALIFIED").toString());
                    unqualified += Integer.valueOf(map.get("UNQUALIFIED").toString());
                }
                resultMap.put("QUALIFIED", qualified);
                resultMap.put("UNQUALIFIED", unqualified);
            } else {
                resultMap.put("QUALIFIED", "--");
                resultMap.put("UNQUALIFIED", "--");
            }
        }
        // 判断最新的一次质控数据时间距离目前多久
        if (dataLatestDate != null) {
            resultMap.put("DATATODATE", DateUtil.hourComparePrecise(dataLatestDate, new Date()));
        } else {
            resultMap.put("DATATODATE", "--");
        }
        if (setUpLatestDate != null) {
            resultMap.put("LATESTTIME", DateUtil.dateToString(setUpLatestDate, "yyyy年MM月dd日")); // 最新质控参数设置的时间
            resultMap.put("TODATE", DateUtil.dayComparePrecise(setUpLatestDate, new Date())); // 最新质控参数设置的时间距离现在多久
        } else {
            resultMap.put("LATESTTIME", "--");
            resultMap.put("TODATE", "--");
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setInfo(resultMap);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Map<String, Map<String, Object>>> currentAllDayQualityControlParam() {
        List<DayQualityControl> list = IDayQualityControlRepository.getAllLatestData();
        // 封装数据
        List<Map<String, Map<String, Object>>> resultList = new ArrayList<Map<String, Map<String, Object>>>();
        if (list.size() == 0) {
            return resultList;
        }
        Map<String, Object> isMap = new HashMap<String, Object>();
        for (DayQualityControl dayQualityControl : list) {
            String id = dayQualityControl.getId();
            String stationCode = dayQualityControl.getStationCode();
            String indexCode = dayQualityControl.getIndexCode();
            String spanValue = dayQualityControl.getSpanValue() == null ? null : String.valueOf(dayQualityControl.getSpanValue()); // 获取配置的跨度值
            String zeroStandardValue = dayQualityControl.getZeroStandardValue() == null ? null
                    : String.valueOf(dayQualityControl.getZeroStandardValue()); // 获取配置的零点标准溶液浓度
            String spanStandardValue = dayQualityControl.getSpanStandardValue() == null ? null
                    : String.valueOf(dayQualityControl.getSpanStandardValue()); // 获取配置的跨度标准溶液浓度
            if (!isMap.containsKey(stationCode)) {
                // 初始化存入各站点数据集
                Map<String, Map<String, Object>> stationMap = new HashMap<String, Map<String, Object>>();
                // 初始化存入各站点下的所有因子的跨度值数据集
                Map<String, Object> indexMap = new HashMap<String, Object>();
                indexMap.put(indexCode + "id", id);
                indexMap.put(indexCode + "spanValue", spanValue);
                indexMap.put(indexCode + "zeroStandardValue", zeroStandardValue);
                indexMap.put(indexCode + "spanStandardValue", spanStandardValue);
                stationMap.put(stationCode, indexMap);
                isMap.put(stationCode, stationMap);
                resultList.add(stationMap);
            } else {
                Map<String, Map<String, Object>> stationMap = (Map<String, Map<String, Object>>) isMap.get(stationCode);
                Map<String, Object> indexMap = stationMap.get(stationCode);
                indexMap.put(indexCode + "id", id);
                indexMap.put(indexCode + "spanValue", spanValue);
                indexMap.put(indexCode + "zeroStandardValue", zeroStandardValue);
                indexMap.put(indexCode + "spanStandardValue", spanStandardValue);
            }
        }
        return resultList;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Map<String, Map<String, Object>>> currentWeekQualityControlParam() {
        List<WeekQualityControl> list = IWeekQualityControlRepository.getAllLatestData();
        // 封装数据
        List<Map<String, Map<String, Object>>> resultList = new ArrayList<Map<String, Map<String, Object>>>();
        if (list.size() == 0) {
            return resultList;
        }
        Map<String, Object> isMap = new HashMap<String, Object>();
        for (WeekQualityControl weekQualityControl : list) {
            String id = weekQualityControl.getId();
            String stationCode = weekQualityControl.getStationCode();
            String indexCode = weekQualityControl.getIndexCode();
            // 获取标准液浓度
            String standardValue = weekQualityControl.getStandardValue() == null ? null
                    : String.valueOf(weekQualityControl.getStandardValue());
            if (!isMap.containsKey(stationCode)) {
                // 初始化存入各站点数据集
                Map<String, Map<String, Object>> stationMap = new HashMap<String, Map<String, Object>>();
                // 初始化存入各站点下的所有因子的标准液浓度
                Map<String, Object> indexMap = new HashMap<String, Object>();
                indexMap.put(indexCode + "id", id);
                indexMap.put(indexCode, standardValue);
                stationMap.put(stationCode, indexMap);
                isMap.put(stationCode, stationMap);
                resultList.add(stationMap);
            } else {
                Map<String, Map<String, Object>> stationMap = (Map<String, Map<String, Object>>) isMap.get(stationCode);
                Map<String, Object> indexMap = stationMap.get(stationCode);
                indexMap.put(indexCode + "id", id);
                indexMap.put(indexCode, standardValue);
            }
        }
        return resultList;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Map<String, Map<String, Object>>> currentMonthQualityControlRecoveryParam() {
        List<MonthQualityRecovery> list = IMonthQualityRecoveryRepository.getAllLatestData();
        // 封装数据
        List<Map<String, Map<String, Object>>> resultList = new ArrayList<Map<String, Map<String, Object>>>();
        if (list.size() == 0) {
            return resultList;
        }
        Map<String, Object> isMap = new HashMap<String, Object>();
        for (MonthQualityRecovery monthQualityRecovery : list) {
            String id = monthQualityRecovery.getId();
            String stationCode = monthQualityRecovery.getStationCode();
            String indexCode = monthQualityRecovery.getIndexCode();
            // 获取配置的加标样浓度
            String addConcentration = monthQualityRecovery.getAddConcentration() == null ? null
                    : String.valueOf(monthQualityRecovery.getAddConcentration());
            // 获取配置的定容体积
            String constantVolume = monthQualityRecovery.getConstantVolume() == null ? null
                    : String.valueOf(monthQualityRecovery.getConstantVolume());
            if (!isMap.containsKey(stationCode)) {
                // 初始化存入各站点数据集
                Map<String, Map<String, Object>> stationMap = new HashMap<String, Map<String, Object>>();
                // 初始化存入各站点下的所有因子的加标样浓度与定容体积数据集
                Map<String, Object> indexMap = new HashMap<String, Object>();
                indexMap.put(indexCode + "id", id);
                indexMap.put(indexCode + "addConcentration", addConcentration);
                indexMap.put(indexCode + "constantVolume", constantVolume);
                stationMap.put(stationCode, indexMap);
                isMap.put(stationCode, stationMap);
                resultList.add(stationMap);
            } else {
                Map<String, Map<String, Object>> stationMap = (Map<String, Map<String, Object>>) isMap.get(stationCode);
                Map<String, Object> indexMap = stationMap.get(stationCode);
                indexMap.put(indexCode + "id", id);
                indexMap.put(indexCode + "addConcentration", addConcentration);
                indexMap.put(indexCode + "constantVolume", constantVolume);
            }
        }
        return resultList;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Map<String, Map<String, Object>>> currentMonthQualityControlStandardCheckParam() {
        List<MonthQualityStandardCheck> list = IMonthQualityStandardCheckRepository.getAllLatestData();
        // 封装数据
        List<Map<String, Map<String, Object>>> resultList = new ArrayList<Map<String, Map<String, Object>>>();
        if (list.size() == 0) {
            return resultList;
        }
        Map<String, Object> isMap = new HashMap<String, Object>();
        for (MonthQualityStandardCheck monthQualityStandardCheck : list) {
            String stationCode = monthQualityStandardCheck.getStationCode();
            String indexCode = monthQualityStandardCheck.getIndexCode();
            // 获取配置的标准溶液浓度
            String standardValue = monthQualityStandardCheck.getStandardValue() == null ? null
                    : String.valueOf(monthQualityStandardCheck.getStandardValue());
            if (!isMap.containsKey(stationCode)) {
                // 初始化存入各站点数据集
                Map<String, Map<String, Object>> stationMap = new HashMap<String, Map<String, Object>>();
                // 初始化存入各站点下的所有因子的加标样浓度与定容体积数据集
                Map<String, Object> indexMap = new HashMap<String, Object>();
                indexMap.put(indexCode, standardValue);
                stationMap.put(stationCode, indexMap);
                isMap.put(stationCode, stationMap);
                resultList.add(stationMap);
            } else {
                Map<String, Map<String, Object>> stationMap = (Map<String, Map<String, Object>>) isMap.get(stationCode);
                Map<String, Object> indexMap = stationMap.get(stationCode);
                indexMap.put(indexCode, standardValue);
            }
        }
        return resultList;
    }

}
